clearvars;

% =========================================================================
% 生成1500幅板状结构图像，板的个数，板的长宽，板的位置，板的强度，板的倾斜角度都是变量
% =========================================================================

% create the computational grid
Nx = 534;  % number of grid points in the x (row) direction
Ny = 268;  % number of grid points in the y (column) direction

% create initial pressure distribution using makeDisc

num_images = 1500; % 要生成的图像数量

for img_idx = 1 : num_images

    % create initial pressure distribution
    source.p0 = zeros(Nx, Ny); % 初始化压力分布为零矩阵

    num_of_plates = randi([1, 2], 1, 1);   % 板的个数是1到2之间的随机数

    for point_idx = 1 : num_of_plates
        % 随机生成每个点的位置
        start_point = [randi([100, Nx-50], 1, 1), 1]; % 生成1行2列的随机整数矩阵，范围在5到Nx-5之间，作为点的位置
        if mod(img_idx, 2) == 0
            angle = pi + pi * rand(1, 1) * 0.1;
        else
            angle = pi - pi * rand(1, 1) * 0.1;
        end
        
        %angle = 0;
        % 随机生成每个点的大小和强度
        length = randi([10, 80], 1, 1) + Ny; % 板的长度贯穿所有的列
        width = randi([3, 20], 1, 1);  % 板的宽度是5到15之间的随机数
        disc_magnitude = randi([150, 255], 1, 1); % 强度是150到255之间的随机数[Pa]
        
        rect = zeros(Nx, Ny); % 初始化压力分布为零矩阵
        for i = 1 : width
            new_start_point = [round(start_point(1) + cos(angle)), round(start_point(2) + sin(angle))];
            % 检查 new_start_point 是否在 [1, 1] 和 [Nx, Ny] 范围内
            if new_start_point(1) >= 1 && new_start_point(1) <= Nx && new_start_point(2) >= 1 && new_start_point(2) <= Ny
                % 如果在范围内，则使用 new_start_point
                start_point = new_start_point;
            else
                % 如果超出范围，则跳出循环或采取其他措施
                break;
            end
%             start_point = [50, 1];
%             angle = pi + 0.1;
%             length = 200;
            line = disc_magnitude * makeLine(Nx, Ny, start_point, angle, length);
            rect = rect + line;
        end
        % 将板状结构叠加到初始压力分布上
        source.p0 = source.p0 + rect;
    end
    % smooth the initial pressure distribution and restore the magnitude
    source.p0 = smooth(source.p0, true);

    % ==================================================================
    % SAVE RESULTS，需要保存ground-truth图像
    % ==================================================================
    % 定义保存路径和文件名
    filename = sprintf('./PLATES_Dataset/plate_initial_pressure_%04d.png', img_idx);
    
    % 将 source.p0 的数据范围缩放到 [0, 255] 以便于保存为图像
    scaled_p0 = mat2gray(source.p0);
    
    % 使用 imwrite 函数保存图像
    imwrite(uint8(scaled_p0 * 255), filename);
    
    % =========================================================================
    % VISUALISATION
    % =========================================================================
    % plot the initial pressure and sensor distribution
%     figure;
%     imagesc(scaled_p0);
%     colormap(getColorMap);
end

% % 板状结构
% disc_magnitude = 20;
% angle = pi;
% length = 40;
% thickness = 10;
% start_point = [49, 30];
% source.p0 = zeros(Nx, Ny);
% for i = 1 : thickness
%     start_point = [49+i, 30];
%     line = disc_magnitude * makeLine(Nx, Ny, start_point, angle, length);
%     source.p0 = source.p0 + line;
% end


